//user_server的测试客户端实现
#include <gflags/gflags.h>
#include <gtest/gtest.h>
#include <thread>
#include "etcd.hpp"
#include "channel.hpp" 
#include "utils.hpp"
#include "base.pb.h"
#include "friend.pb.h"
//日志类参数
DEFINE_bool(log_mod,false,"true为发布模式,false为调试模式");
DEFINE_string(log_file,"","发布模式下日志存储的文件");
DEFINE_int32(log_level,0,"要输出的日志等级");
//服务注册参数
DEFINE_string(etcd_host,"http://127.0.0.1:2379","服务注册中心地址");
DEFINE_string(basedir,"/service","服务查询的根目录");
DEFINE_string(friend_service,"/service/friend_service","当前实例名称");
zzy_mims::ServiceChannels::ChannelPtr channel;
namespace zzy_mims
{


void apply(const std::string& sender_id,const std::string& accepter_id)//发送好友申请
{
     //1.获取rpc服务调用句柄
     ::zzy_mims::FriendService_Stub stub(channel.get());
     //2.填写请求
     zzy_mims::FriendAddReq request;
     zzy_mims::FriendAddRsp response;
     
     request.set_request_id(zzy_mims::uuid());
     request.set_user_id(sender_id);
     request.set_respondent_id(accepter_id);
     
     //3.调用服务
     brpc::Controller cntl;
     stub.FriendAdd(&cntl,&request,&response,nullptr);//同步调用
     //4.验证应答是否符合预期
     ASSERT_FALSE(cntl.Failed());
     ASSERT_TRUE(response.success());
}
void getApplyList(const std::string& uid)//获取好友列表
{
     //1.获取rpc服务调用句柄
     ::zzy_mims::FriendService_Stub stub(channel.get());
     //2.填写请求
     zzy_mims::GetPendingFriendEventListReq request;
     zzy_mims::GetPendingFriendEventListRsp response;
     
     request.set_request_id(zzy_mims::uuid());
     request.set_user_id(uid);
     
     //3.调用服务
     brpc::Controller cntl;
     stub.GetPendingFriendEventList(&cntl,&request,&response,nullptr);//同步调用
     //4.验证应答是否符合预期
     ASSERT_FALSE(cntl.Failed());
     ASSERT_TRUE(response.success());
     std::cout<<"getApplyList:"<<std::endl;
    for (int i = 0; i < response.event_size(); i++) {
        std::cout << response.event(i).sender().user_id() << std::endl;
        std::cout << response.event(i).sender().nickname() << std::endl;
        std::cout << response.event(i).sender().avatar() << std::endl;
        std::cout << "---------------\n";
    }
}
void processApply(const std::string& sender_id,const std::string& accepter_id,bool isAgree)//处理好友申请
{
     //1.获取rpc服务调用句柄
     ::zzy_mims::FriendService_Stub stub(channel.get());
     //2.填写请求
     zzy_mims::FriendAddProcessReq request;
     zzy_mims::FriendAddProcessRsp response;
     
     request.set_request_id(zzy_mims::uuid());
     request.set_apply_user_id(sender_id);
     request.set_user_id(accepter_id);

     request.set_agree(isAgree);
     
     //3.调用服务
     brpc::Controller cntl;
     stub.FriendAddProcess(&cntl,&request,&response,nullptr);//同步调用
     //4.验证应答是否符合预期
     ASSERT_FALSE(cntl.Failed());
     ASSERT_TRUE(response.success());
     if (isAgree) {
        std::cout<<"单聊:"<<std::endl;
        std::cout << response.new_session_id() << std::endl;
    }
}
void getNoneFriendList(const std::string& uid,const std::string& key)//使用关键字查询非好友列表
{
     //1.获取rpc服务调用句柄
     ::zzy_mims::FriendService_Stub stub(channel.get());
     //2.填写请求
     zzy_mims::FriendSearchReq request;
     zzy_mims::FriendSearchRsp response;
     
     request.set_request_id(zzy_mims::uuid());
     request.set_user_id(uid);
     request.set_search_key(key);
     
     //3.调用服务
     brpc::Controller cntl;
     stub.FriendSearch(&cntl,&request,&response,nullptr);//同步调用
     //4.验证应答是否符合预期
     ASSERT_FALSE(cntl.Failed());
     ASSERT_TRUE(response.success());
     std::cout<<"getNoneFriendList:"<<std::endl;
     for (int i = 0; i < response.user_info_size(); i++) {
        std::cout << response.user_info(i).user_id() << std::endl;
        std::cout << response.user_info(i).nickname() << std::endl;
        std::cout << response.user_info(i).avatar() << std::endl;
        std::cout << "-------------------\n";
    }
}
void removeFriend(const std::string& uid1,const std::string& uid2)//删除好友
{
     //1.获取rpc服务调用句柄
     ::zzy_mims::FriendService_Stub stub(channel.get());
     //2.填写请求
     zzy_mims::FriendRemoveReq request;
     zzy_mims::FriendRemoveRsp response;
     
     request.set_request_id(zzy_mims::uuid());
     request.set_user_id(uid1);
     request.set_peer_id(uid2);
     
     //3.调用服务
     brpc::Controller cntl;
     stub.FriendRemove(&cntl,&request,&response,nullptr);//同步调用
     //4.验证应答是否符合预期
     ASSERT_FALSE(cntl.Failed());
     ASSERT_TRUE(response.success());
}
void getFriendList(const std::string& uid)
{
    //1.获取rpc服务调用句柄
    ::zzy_mims::FriendService_Stub stub(channel.get());
    //2.填写请求
    zzy_mims::GetFriendListReq request;
    zzy_mims::GetFriendListRsp response;
    
    request.set_request_id(zzy_mims::uuid());
    request.set_user_id(uid);
    
    //3.调用服务
    brpc::Controller cntl;
    stub.GetFriendList(&cntl,&request,&response,nullptr);//同步调用
    //4.验证应答是否符合预期
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(response.success());
    std::cout<<"getFriendList:"<<std::endl;
    for (int i = 0; i < response.friend_list_size(); i++) {
        std::cout << response.friend_list(i).user_id() << std::endl;
        std::cout << response.friend_list(i).nickname() << std::endl;
        std::cout << response.friend_list(i).avatar() << std::endl;
        std::cout << "-------------------\n";

    }

}
void buildChatSession(const std::string& builder_id,const std::vector<std::string>& members_id_list,const std::string& sname)//创建群聊
{
     //1.获取rpc服务调用句柄
     ::zzy_mims::FriendService_Stub stub(channel.get());
     //2.填写请求
     zzy_mims::ChatSessionCreateReq request;
     zzy_mims::ChatSessionCreateRsp response;
     
     request.set_request_id(zzy_mims::uuid());
     request.set_user_id(builder_id);
     request.set_chat_session_name(sname);
     for(auto& mid:members_id_list)
     request.add_member_id_list(mid);
     
     //3.调用服务
     brpc::Controller cntl;
     stub.ChatSessionCreate(&cntl,&request,&response,nullptr);//同步调用
     //4.验证应答是否符合预期
     ASSERT_FALSE(cntl.Failed());
     ASSERT_TRUE(response.success());
    std::cout<<"群聊:"<<std::endl;
    std::cout << response.chat_session_info().chat_session_id() << std::endl;
    std::cout << response.chat_session_info().chat_session_name() << std::endl;
}
void getChatSessionList(const std::string& uid)//获取会话列表
{
     //1.获取rpc服务调用句柄
     ::zzy_mims::FriendService_Stub stub(channel.get());
     //2.填写请求
     zzy_mims::GetChatSessionListReq request;
     zzy_mims::GetChatSessionListRsp response;
     
     request.set_request_id(zzy_mims::uuid());
     request.set_user_id(uid);
     
     //3.调用服务
     brpc::Controller cntl;
     stub.GetChatSessionList(&cntl,&request,&response,nullptr);//同步调用
     //4.验证应答是否符合预期
     ASSERT_FALSE(cntl.Failed());
     ASSERT_TRUE(response.success());
    std::cout<<"getChatSessionList:"<<std::endl;
     for (int i = 0; i < response.chat_session_info_list_size(); i++) {
        std::cout << "-------------------\n";
        std::cout << response.chat_session_info_list(i).single_chat_friend_id() << std::endl;
        std::cout << response.chat_session_info_list(i).chat_session_id() << std::endl;
        std::cout << response.chat_session_info_list(i).chat_session_name() << std::endl;
        std::cout << response.chat_session_info_list(i).avatar() << std::endl;
        std::cout << "消息内容：\n";
        std::cout << response.chat_session_info_list(i).prev_message().message_id() << std::endl;
        std::cout << response.chat_session_info_list(i).prev_message().chat_session_id() << std::endl;
        std::cout << response.chat_session_info_list(i).prev_message().timestamp() << std::endl;
        std::cout << response.chat_session_info_list(i).prev_message().sender().user_id() << std::endl;
        std::cout << response.chat_session_info_list(i).prev_message().sender().nickname() << std::endl;
        std::cout << response.chat_session_info_list(i).prev_message().sender().avatar() << std::endl;
        std::cout << response.chat_session_info_list(i).prev_message().message().file_message().file_name() << std::endl;
        std::cout << response.chat_session_info_list(i).prev_message().message().file_message().file_contents() << std::endl;
    }
}
void getSessionMember(const std::string& sid)//获取指定会话的所有成员
{
     //1.获取rpc服务调用句柄
     ::zzy_mims::FriendService_Stub stub(channel.get());
     //2.填写请求
     zzy_mims::GetChatSessionMemberReq request;
     zzy_mims::GetChatSessionMemberRsp response;
     
     request.set_request_id(zzy_mims::uuid());
     request.set_chat_session_id(sid);
     
     //3.调用服务
     brpc::Controller cntl;
     stub.GetChatSessionMember(&cntl,&request,&response,nullptr);//同步调用
     //4.验证应答是否符合预期
     ASSERT_FALSE(cntl.Failed());
     ASSERT_TRUE(response.success());
     std::cout<<"getSessionMember:"<<std::endl;
     for (int i = 0; i < response.member_info_list_size(); i++) 
     {
        std::cout << "-------------------\n";
        std::cout << response.member_info_list(i).user_id() << std::endl;
        std::cout << response.member_info_list(i).nickname() << std::endl;
        std::cout << response.member_info_list(i).avatar() << std::endl;
    }
}
}




//测试流程:测试人员2,3,4向1申请好友，1向5申请好友
//1获取申请事件列表（应该有3个）
//1同意2,3，拒绝4
//1查询用户列表（应该会查询到4,5）
//1删除3好友
//1获取好友列表（应该会查询到2）
//1建立包括1,2,3,4的群聊
//1给2发消息，在群聊发消息
//2获取聊天会话列表（应该是会有两个，一个群聊，一个和1的单聊）
//获取群聊的成员

int main(int argc,char* argv[])
{ 
    testing::InitGoogleTest(&argc, argv);

    google::ParseCommandLineFlags(&argc,&argv,true);
    zzy_mims::init_logger(FLAGS_log_mod,FLAGS_log_file,(spdlog::level::level_enum)FLAGS_log_level);
    //关闭brpc的默认日志输出
    logging::LoggingSettings logset;
    logset.logging_dest = logging::LoggingDestination::LOG_TO_NONE;
    logging::InitLogging(logset);

    //1.创建RPC信道管理对象
    auto scm = std::make_shared<zzy_mims::ServChanManager>();
    scm->Declared(FLAGS_friend_service);
    //2.构建服务发现对象
    auto online = std::bind(&zzy_mims::ServChanManager::ServiceOnline,scm.get(),std::placeholders::_1,std::placeholders::_2);
    auto offline = std::bind(&zzy_mims::ServChanManager::ServiceOffline,scm.get(),std::placeholders::_1,std::placeholders::_2);
    std::shared_ptr<zzy_mims::Discovery> dclient = std::make_shared<zzy_mims::Discovery>(FLAGS_etcd_host,FLAGS_basedir,online,offline); 
    //获取信道
    channel = scm->choose(FLAGS_friend_service);
    if(!channel)
    {
        LOG_ERROR("获取用户rpc服务调用信道失败\n");
        return -1;
    };
    const std::string uid1 ="6559d28e9440-0000";
    const std::string uid2 ="158998af4180-0001";
    const std::string uid3 ="5966d78949e3-0002";
    const std::string uid4 ="3892b567a3bb-0003";
    const std::string uid5 ="22cb9cc825db-0004";

    // zzy_mims::apply(uid2,uid1);//2->1
    // zzy_mims::apply(uid3,uid1);//3->1
    // zzy_mims::apply(uid4,uid1);//4->1
    // zzy_mims::apply(uid1,uid5);//1->5
    // zzy_mims::getApplyList(uid1);
    // zzy_mims::processApply(uid2,uid1,true);
    // zzy_mims::processApply(uid3,uid1,true);
    // zzy_mims::processApply(uid4,uid1,false);
    // zzy_mims::getNoneFriendList(uid1,"测试");
    // zzy_mims::removeFriend(uid3,uid1);
    // zzy_mims::getFriendList(uid1);
    // zzy_mims::buildChatSession(uid1,{uid1,uid2,uid3,uid4},"测试人员专属群");
    //zzy_mims::getChatSessionList(uid2);
    zzy_mims::getSessionMember("1286c6aa3fe4-0006");
    return 0;
}